/*
Copyright (c) 2020-2021, Huawei Technologies Co., Ltd
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* Description: 3D图表可视化插件数据组织类
*/
#include "slt_space_data.h"


namespace chart_plugin {
using std::vector;
uint32_t SltObject::id = 0;

bool SltSpaceData::SetCurveData(const vector<custom::SltPoint> &pointVector, vector<mglPoint> *mglPointVector) const
{
    if (mglPointVector == nullptr) {
        return false;
    }

    if (!mglPointVector->empty()) {
        mglPointVector->clear();
    }

    for (const auto &point : pointVector) {
        (*mglPointVector).push_back(mglPoint(point.sValue, point.lValue, point.tValue));
    }

    return true;
}

vector<mglPoint> SltSpaceData::CreateObjectIdData(const custom::SltPoint &point)
{
    vector<mglPoint> objectIdData;
    objectIdData.push_back(mglPoint(point.sValue, point.lValue, point.tValue));
    return objectIdData;
}

void SltSpaceData::SetObjectsFaceData(const vector<custom::SltPoint> &pointVector)
{
    objectsMap.clear();
    objectsIdTypeMap.clear();
    objectsTypeIdMap.clear();

    if (pointVector.empty()) {
        return;
    }

    QStringList listId;
    for (const auto &point : pointVector) {
        int32_t type = point.type;
        QString id = QString::fromStdString(point.id);
        listId.push_back(id);
        if (!idIdMap.contains(id)) {
            LimitObjectsNumber(listId);
            LimitIdSize();
            QSharedPointer<SltObject> object = QSharedPointer<SltObject>(new SltObject(id));
            idObjectMap.insert(object->sltObjectId, object);
            idIdMap.insert(id, object->sltObjectId);
        }
		
        if (!objectsMap.contains(id)) {
            objectsMap.insert(id, CreateObjectIdData(point));
            objectsIdTypeMap.insert(id, type);
            QStringList listId = objectsTypeIdMap.value(type);
            if (!listId.contains(id)) {
                listId.push_back(id);
            }
            objectsTypeIdMap[type] = listId;
            continue;
        }
        objectsMap[id].push_back(mglPoint(point.sValue, point.lValue, point.tValue));
    }
}

const vector<mglPoint> &SltSpaceData::GetOptimizedSltTrack() const
{
    return optimizedSltTrack;
}

const vector<mglPoint> &SltSpaceData::GetSltSpeedLimit() const
{
    return sltSpeedLimit;
}

const QMap<QString, std::vector<mglPoint>> &SltSpaceData::GetObjectsMap() const
{
    return objectsMap;
}

const QMap<QString, int32_t> &SltSpaceData::GetObjectsIdTypeMap() const
{
    return objectsIdTypeMap;
}

const QMap<int32_t, QStringList> &SltSpaceData::GetObjectsTypeIdMap() const
{
    return objectsTypeIdMap;
}

void SltSpaceData::SetObjectVisible(const QString &id, bool isVisible)
{
    QString sltObjectId = idIdMap.value(id);
    idObjectMap.value(sltObjectId)->isVisible = isVisible;
}

bool SltSpaceData::GetObjectVisible(const QString &id) const
{
    QString sltObjectId = idIdMap.value(id);
    return idObjectMap.value(sltObjectId)->isVisible;
}

void SltSpaceData::Clear()
{
    idObjectMap.clear();
    idIdMap.clear();

    optimizedSltTrack.clear();
    sltSpeedLimit.clear();
    objectsMap.clear();
    objectsIdTypeMap.clear();
    objectsTypeIdMap.clear();
}

bool SltSpaceData::Add(const custom::SltSpace::ConstPtr &msg)
{
    if (msg == nullptr) {
        return false;
    }

    vector<custom::SltPoint> optimizedSltTrackData = msg->optimizedSltTrack;
    vector<custom::SltPoint> sltSpeedLimitData = msg->sltSpeedLimit;
    vector<custom::SltPoint> sltObjectsData = msg->sltObjects;

    if (SetCurveData(optimizedSltTrackData, &optimizedSltTrack) && SetCurveData(sltSpeedLimitData, &sltSpeedLimit)) {
        SetObjectsFaceData(sltObjectsData);
        return true;
    }
    return false;
}

void SltSpaceData::LimitObjectsNumber(const QStringList &listId)
{
    int32_t objectsMaxNumber = 1000;
    if (idObjectMap.count() < objectsMaxNumber) {
        return;
    }

    for (auto iter = idObjectMap.begin(); iter != idObjectMap.end(); iter++) {
        if (!listId.contains(iter.key())) {
            idIdMap.remove(iter.value()->objectId);
            idObjectMap.erase(iter);
            return;
        }
    }
}

void SltSpaceData::LimitIdSize()
{
    if (SltObject::id < UINT_MAX) {
        return;
    }

    QMap<QString, QSharedPointer<SltObject>> map;
    auto objects = idObjectMap.values();
    for (int32_t i = 0; i < objects.count(); i++) {
        auto object = objects.at(i);
        object->setId(i);
        map.insert(QString::number(i), object);
        idIdMap[object->objectId] = QString::number(i);
    }
    idObjectMap.clear();
    idObjectMap.swap(map);
}

} //  namespace chart_plugin
